﻿/*************************************************************************************
     * CLR 版本：       4.0.30319.42000
     * 类 名 称：       CsExcelWrite
     * 机器名称：       KAIKO
     * 命名空间：       PunchCard.Service
     * 文 件 名：       CsExcelWrite
     * 创建时间：       2022/4/11 15:24:58
     * 作    者：       WongJunLiang
     * 说    明：
     * 修改时间：
     * 修 改 人：
*************************************************************************************/


using PunchCard.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PunchCard.Service
{
    class CsExcelWrite
    {
        public static bool ExportLists(string FileName, ObservableCollection<CsListINF> Litem)//保存数据
        {
            if (Litem.Count == 0)
                return false;
            try
            {
                System.IO.FileStream fs = new System.IO.FileStream(FileName, System.IO.FileMode.Create);
                NPOI.SS.UserModel.IWorkbook workbook;
                int index = FileName.LastIndexOf('.');
                string ext = FileName.Substring(index + 1);
                if (ext == "xls")
                    workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
                //else if (ext == "xlsx")
                //    workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                else
                    return false;

                var sheet = workbook.CreateSheet("明细1");
                var row0 = sheet.CreateRow(0);
                row0.CreateCell(0).SetCellValue("序号");
                row0.CreateCell(1).SetCellValue(" 员工姓名##fyuangxm");
                row0.CreateCell(2).SetCellValue(" 签卡日期##fqiankrq");
                row0.CreateCell(3).SetCellValue(" 时间##fshij");
                row0.CreateCell(4).SetCellValue(" 签卡类型##fqianklx");
                row0.CreateCell(5).SetCellValue(" 事由##fshiy");

                for (int i = 0; i < Litem.Count; i++)
                {
                    var row = sheet.CreateRow(i + 1);
                    row.CreateCell(0).SetCellValue(Litem[i].strID);
                    row.CreateCell(1).SetCellValue(Litem[i].strName);
                    row.CreateCell(2).SetCellValue(Litem[i].strDate);
                    row.CreateCell(3).SetCellValue(Litem[i].strTime);
                    row.CreateCell(4).SetCellValue(Litem[i].strType);
                    row.CreateCell(5).SetCellValue(Litem[i].strReason);
                }
                workbook.Write(fs);
                fs.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("失败");
                return false;
            }
        }

        public static void WriteListModelFile(string FileName)//保存数据
        {
            try
            {
                System.IO.FileStream fs = new System.IO.FileStream(FileName, System.IO.FileMode.Create);
                NPOI.SS.UserModel.IWorkbook workbook;
                int index = FileName.LastIndexOf('.');
                string ext = FileName.Substring(index + 1);
                if (ext == "xls")
                    workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
                //else if (ext == "xlsx")
                //    workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                else
                    return;
                var sheet = workbook.CreateSheet("人员名单");
                var row0 = sheet.CreateRow(0);
                row0.CreateCell(0).SetCellValue("姓名");
                row0.CreateCell(1).SetCellValue("工号");
                row0.CreateCell(2).SetCellValue("签卡事由");

                workbook.Write(fs);
                fs.Close();
                MessageBox.Show("批量导入名单不存在，在软件根目录创建成功，请填写", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("失败");
            }
        }

    }
}
